import { Effect, Reducer, Subscription } from 'umi';
import {getArticalList, getCommentList} from '../services/index'
export interface BenchModelState {
  articalList: [];
  commentList: [];
}

export interface BenchModelType {
  namespace: 'bench';
  state: BenchModelState;
  effects: {
    getArticalList: Effect;
    getCommentList: Effect;
  };
  reducers: {
    save: Reducer<BenchModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}

const BenchModel: BenchModelType = {
  namespace: 'bench',

  state: {
    articalList: [],
    commentList:[]
  },

  effects: {
    *getArticalList({ payload }, { call, put }) {
        let result = yield getArticalList()
        yield put({
            type:'save',
            payload:{
              articalList:result.data
            }
        })
    },
    *getCommentList({ payload }, { call, put }) {
        let result = yield getCommentList()
        console.log(11);
        yield put({
          type:'save',
          payload:{
            commentList:result.data
          }
        })
    },
  },

  reducers: {
    save(state, action) {
      console.log(action)
      return {
        ...state,
        ...action.payload,
      };
    },
  }
};

export default BenchModel;